﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Csla;

namespace Asah.SiteWorx.SiteMap
{
    [Serializable()]
    public class PageInfo : Csla.ReadOnlyBase<PageInfo>
    {
        private int _id;
        private string _title;
        private string _fileName;
        private string _url;
        private string _metaKeywords;
        private string _metaDescription;
        private string _menuText;
        private string _menuHoverText;
        private bool _displayInMenu;
        private int _menuPosition;
        private string _masterPage;
        private PageVisibility _visibility;
        private int _parentPageId;
        private bool _isHomePage;
        private LinkedList<PageInfo> _childPages;
        private Membership.User _createdBy;
        private SmartDate _createdDate;
        private Membership.User _lastUpdatedBy;
        private SmartDate _lastUpdatedDate;

        public int ID
        {
            get
            {
                return _id;
            }
            internal set
            {
                _id = value;
            }
        }

        public string Title
        {
            get
            {
                return _title;
            }
            internal set
            {
                _title = value;
            }
        }

        public string FileName
        {
            get
            {
                return _fileName;
            }
            internal set
            {
                _fileName = value;
            }
        }

        public string Url
        {
            get
            {
                return _url;
            }
            internal set
            {
                _url = value;
            }
        }

        public string MetaKeywords
        {
            get
            {
                return _metaKeywords;
            }
            internal set
            {
                _metaKeywords = value;
            }
        }

        public string MetaDescription
        {
            get
            {
                return _metaDescription;
            }
            internal set
            {
                _metaDescription = value;
            }
        }

        public string MenuText
        {
            get
            {
                return _menuText;
            }
            internal set
            {
                _menuText = value;
            }
        }

        public string MenuHoverText
        {
            get
            {
                return _menuHoverText;
            }
            internal set
            {
                _menuHoverText = value;
            }
        }

        public bool DisplayInMenu
        {
            get
            {
                return _displayInMenu;
            }
            internal set
            {
                _displayInMenu = value;
            }
        }

        public int MenuPosition
        {
            get
            {
                return _menuPosition;
            }
            internal set
            {
                _menuPosition = value;
            }
        }

        public string MasterPage
        {
            get
            {
                return _masterPage;
            }
            internal set
            {
                _masterPage = value;
            }
        }

        public PageVisibility Visibility
        {
            get
            {
                return _visibility;
            }
            internal set
            {
                _visibility = value;
            }
        }

        public int ParentPageId
        {
            get
            {
                return _parentPageId;
            }
            internal set
            {
                _parentPageId = value;
            }
        }

        public bool IsHomePage
        {
            get
            {
                return _isHomePage;
            }
            internal set
            {
                _isHomePage = value;
            }
        }

        public LinkedList<PageInfo> ChildPages
        {
            get
            {
                return _childPages;
            }
            internal set
            {
                _childPages = value;
            }
        }

        public Membership.User CreatedBy
        {
            get
            {
               return _createdBy;
            }
        }

        public string CreatedDate
        {
            get
            {
                return _createdDate.ToString("g");
            }
            internal set
            {
                _createdDate.Text = value;
            }
        }

        public Membership.User LastUpdatedBy
        {
            get
            {
                return _lastUpdatedBy;
            }
        }

        public string LastUpdatedDate
        {
            get
            {
                return _lastUpdatedDate.ToString("g");
            }
            internal set
            {
                _lastUpdatedDate.Text = value;
            }
        }

        public override string ToString()
        {
            return _fileName;
        }

        protected override object GetIdValue()
        {
            return _id;
        }

        private PageInfo()
        {
            // require use of factory methods
        }

        internal PageInfo(int id, string title, string fileName, string url, string metaKeywords,
                          string metaDescription, string menuText, string menuHoverText, bool displayInMenu,
                          int menuPosition, string masterPage, PageVisibility visibility, int parentPageId,
                          Guid createdBy, SmartDate createdDate, Guid lastUpdatedBy, SmartDate lastUpdatedDate)
        {
           _id = id;
            _title = title;
            _fileName = fileName;
            _url = url;
            _metaKeywords = metaKeywords;
            _metaDescription = metaDescription;
            _menuText = menuText;
            _menuHoverText = menuHoverText;
            _displayInMenu = displayInMenu;
            _menuPosition = menuPosition;
            _masterPage = masterPage;
            _visibility = visibility;
            _parentPageId = parentPageId;
            _isHomePage = IsDefaultPage(_fileName);
            _createdBy = Membership.User.GetUser(createdBy);
            _createdDate = createdDate;
            _lastUpdatedBy = Membership.User.GetUser(lastUpdatedBy);
            _lastUpdatedDate = lastUpdatedDate;
            _childPages = new LinkedList<PageInfo>();
        }

        private static bool IsDefaultPage(string fileName)
        {
            return fileName.ToUpper() == "DEFAULT.ASPX";
        }
                
    }
}
